//
//  XSTextView.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/12.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit
@IBDesignable


class XSTextView: UITextView {

    @IBInspectable var placeholder:String?{
        didSet{
            placeHolderLabel.text = placeholder
        }
    }
    
    override var font:UIFont?{
        didSet{
            placeHolderLabel.font = font
        }
    }
    
    override var text:String?{
        didSet{
            placeHolderLabel.hidden = hasText()
        }
    }
    
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(placeHolderLabel)
        placeHolderLabel.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(SCREENW - 10)
        }
        
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    }
    
    
    //文本框有变化时,placeHolder隐藏
    @objc private func textDidChange(){
        
        placeHolderLabel.hidden = hasText()
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    //懒加载空间
    private lazy var placeHolderLabel:UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.lightGrayColor()
        
        label.font = UIFont.systemFontOfSize(16)
        label.text = "这里是一个PlaceHolder"
        label.numberOfLines = 0
        
        return label
    }()
}


